package com.hc.fdkgoods.dao;

import com.hc.fdkgoods.common.page.PageContentContainer;
import com.hc.fdkgoods.vo.ForwarderWholeVO_1;
import com.hc.fdkgoods.vo.vo_3.PageContentInputVO_3;
import com.hc.fdkgoods.vo.vo_3.SKUItemVO;
import org.jooq.SelectConditionStep;
import org.jooq.SelectOnConditionStep;
import org.springframework.stereotype.Repository;

import static com.hc.fdkgoods.db.tables.SellerSku.SELLER_SKU;
import static com.hc.fdkgoods.db.tables.SellerSpu.SELLER_SPU;
import static com.hc.fdkgoods.db.tables.ForwarderSupplier.FORWARDER_SUPPLIER;
import static com.hc.fdkgoods.db.tables.MapSellerSkuSupplier.MAP_SELLER_SKU_SUPPLIER;
import static com.hc.fdkgoods.db.tables.SellerToPrepareSkuList.SELLER_TO_PREPARE_SKU_LIST;

@Repository
public class WaitPushPageDao extends BaseDao{

    private SelectOnConditionStep<?> getQuery(){
        return db.select(
                SELLER_SPU.GOODS_LINK, SELLER_SPU.GOODS_NO, SELLER_SPU.MAIN_IMG_URL,
                SELLER_SPU.GOODS_ID, SELLER_SPU.ID.as("spuId"), SELLER_SPU.SPU_GOODS_NO,
                SELLER_SPU.SPU_NAME, FORWARDER_SUPPLIER.GROUP_NAME.as("supplierGroupName"),
                FORWARDER_SUPPLIER.SUPPLIER_NAME, MAP_SELLER_SKU_SUPPLIER.SUPPLIER_ID,
                MAP_SELLER_SKU_SUPPLIER.PURCHASE_PRICE, MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID,
                SELLER_SKU.SKU_NAME, SELLER_TO_PREPARE_SKU_LIST.SKU_NUM.as("orderSkuNum"),
                SELLER_TO_PREPARE_SKU_LIST.PAY_TIME
        )
                .from(SELLER_SPU)
                .innerJoin(SELLER_SKU)
                .on(SELLER_SPU.ID.eq(SELLER_SKU.SPU_ID))
                .innerJoin(MAP_SELLER_SKU_SUPPLIER)
                .on(SELLER_SKU.ID.eq(MAP_SELLER_SKU_SUPPLIER.SELLER_SKU_ID))
                .innerJoin(FORWARDER_SUPPLIER)
                .on(MAP_SELLER_SKU_SUPPLIER.SUPPLIER_ID.eq(FORWARDER_SUPPLIER.ID))
                .innerJoin(SELLER_TO_PREPARE_SKU_LIST)
                .on(FORWARDER_SUPPLIER.ID.eq(SELLER_TO_PREPARE_SKU_LIST.SUPPLIER_ID));
    }

    public PageContentContainer<SKUItemVO> getSkuItem(PageContentInputVO_3 pageContentInputVO_3){
        SelectOnConditionStep<?> query = getQuery();
        // judge city and market, handle specially
        if(!pageContentInputVO_3.getCity().isEmpty()) {
                    query.where(FORWARDER_SUPPLIER.CITY.eq(pageContentInputVO_3.getCity()));
            if(!pageContentInputVO_3.getMarket().isEmpty()){
                query.and(FORWARDER_SUPPLIER.MARKET.eq(pageContentInputVO_3.getMarket()));
            }
        } else if(!pageContentInputVO_3.getMarket().isEmpty()){
            query.where(FORWARDER_SUPPLIER.MARKET.eq(pageContentInputVO_3.getMarket()));
        }

        if(!pageContentInputVO_3.getSkuIdList().isEmpty()){
            for (Long id:pageContentInputVO_3.getSkuIdList()) {
                query.and(SELLER_SKU.ID.eq(id));
            }
        }

        if(!pageContentInputVO_3.getFloor().isEmpty()){
            query.and(FORWARDER_SUPPLIER.FLOOR.eq(pageContentInputVO_3.getFloor()));
        }

        if(!pageContentInputVO_3.getGoodsNo().isEmpty()){
            query.and(SELLER_SPU.GOODS_NO.like("%"+pageContentInputVO_3.getGoodsNo()+"%"));
        }

        if(!pageContentInputVO_3.getSpuGoodsNo().isEmpty()){
            query.and(SELLER_SPU.SPU_GOODS_NO.like("%" + pageContentInputVO_3.getSpuGoodsNo() + "%"));
        }

        if(pageContentInputVO_3.getSpuId() != 0){
            query.and(SELLER_SPU.ID.eq(pageContentInputVO_3.getSpuId()));
        }

        if(!pageContentInputVO_3.getSpuName().isEmpty()){
            query.and(SELLER_SPU.SPU_NAME.like("%" + pageContentInputVO_3.getSpuName() + "%"));
        }

        if(!pageContentInputVO_3.getSupplierIds().isEmpty()){
            for (Long id:pageContentInputVO_3.getSupplierIds()) {
                query.and(FORWARDER_SUPPLIER.ID.eq(id));
            }
        }

        if(!pageContentInputVO_3.getSupplierName().isEmpty()){
            query.and(FORWARDER_SUPPLIER.SUPPLIER_NAME.eq(pageContentInputVO_3.getSupplierName()));
        }

        query.groupBy(FORWARDER_SUPPLIER.MARKET)
        .orderBy(FORWARDER_SUPPLIER.MARKET.asc(), FORWARDER_SUPPLIER.ID.asc(), SELLER_SPU.SPU_NAME.asc());

        return getPageResult(query, pageContentInputVO_3, SKUItemVO.class);
    }
}
